package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.annotation.EnumVail;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/19）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class EnumValidata implements ValidateInterface {
    @Override
    public <T, E, C> boolean apply(T t, E e, C c) {
        Field f=(Field) e;
        String val= (String) t;
        EnumVail vail=f.getAnnotation(EnumVail.class);
        if(vail != null)
        {
            Class enumClas=vail.enumClass();
            try {
                Method m=enumClas.getMethod(vail.valMethod(),String.class);
                String vl= (String) m.invoke(null,val);
                if(vl == null)
                {
                    return false;
                }
                BeanUtils.setProperty(c,f.getName(),vl);
                return true;
            } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
            return false;
        }
        return true;
    }

    @Override
    public String getName() {
        return null;
    }
}
